class EventsController < ApplicationController
  # GET /events
  # GET /events.xml
  def index
    @events = Event.paginate :page => params[:page], :per_page => 10

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @events }
    end
  end

  # GET /events/1
  # GET /events/1.xml
  def show
    @event = Event.find(params[:id])
  end

  # GET /events/1/edit
  def show_edit
    @event = Event.find(params[:id])
    render :partial=>'edit_event_form',:locals=>{:event=>@event}
  end

  def hide_edit
    render :text=>'';
  end

  # POST /events
  # POST /events.xml
  def create
    @event = Event.new(params[:event])
    @event.add_must_work_items
    @event.save
  end

  # PUT /events/1
  # PUT /events/1.xml
  def update
    @event = Event.find(params[:id])

    @event.update_attributes(params[:event])
  end

  # DELETE /events/1
  # DELETE /events/1.xml
  def destroy
    @event = Event.find(params[:id])
    @event.destroy

    respond_to do |format|
      format.html { redirect_to(events_url) }
      format.xml  { head :ok }
    end
  end

  def query_users_by_category
    arr = params[:id].split(',')
    @event=Event.find(arr[0])
    if arr[1]
      @conventioners = @event.conventioners.paginate_all_by_category arr[1], :page => params[:page], :per_page => 5
    else
      @conventioners = @event.conventioners.paginate :page => params[:page], :per_page => 5
    end
    unless (params[:page])
      render :partial=>'conventioners',:locals=>{:conventioners=>@conventioners,:category=>arr[1]}
    else
      render :update do |page|
        page.replace_html "users_by_会议代表",:partial=>'conventioners',:locals=>{:conventioners=>@conventioners,:category=>arr[1],:page=>params[:page]}
      end
    end
  end

  def query_users_by_leader
    @event=Event.find(params[:id])
    @conventioners=@event.conventioners.paginate_all_by_is_leader true,:page => params[:page], :per_page => 5
    unless (params[:page])
      render :partial=>'conventioners',:locals=>{:conventioners=>@conventioners,:category=>"leader"}
    else
      render :update do |page|
        page.replace_html "users_by_leader",:partial=>'conventioners',:locals=>{:conventioners=>@conventioners,:category=>"leader",:page=>params[:page]}
      end
    end
  end

  def query_users
    arr = params[:id].split(',')
    @event=Event.find(arr[0])
    if arr[1]
      @conventioners = @event.conventioners.paginate_all_by_category arr[1], :page => params[:page], :per_page => 5
    else
      @conventioners = @event.conventioners.paginate :page => params[:page], :per_page => 5
    end
    unless (params[:page])
      render :partial=>'events_users/user_list',:locals=>{:conventioners=>@conventioners}
    else
      render :update do |page|
        page.replace_html "show_users",:partial=>'events_users/user_list',:locals=>{:conventioners=>@conventioners,:page=>params[:page]}
      end
    end
  end

  def query_rooms
    arr = params[:id].split(',')
    @event=Event.find(arr[0])
    if arr[1]
      @rooms = @event.rooms.paginate_all_by_room_category arr[1], :page => params[:page], :per_page => 5
    else
      @rooms = @event.rooms.paginate :page => params[:page], :per_page => 5
    end

    unless params[:page]
      render :partial=>'rooms/room_list',:locals=>{:rooms=>@rooms}
    else
      page.replace_html "show_rooms",:partial=>'rooms/room_list',:locals=>{:rooms=>@rooms,:page=>params[:page]}
    end
  end

  def query_dines
    @event = Event.find(params[:id])
    @dines= @event.dines.paginate :page => params[:page], :per_page => 5
    unless params[:page]
      render :partial=>'dines/dine_list',:locals=>{:dines=>@dines}
    else
      page.replace_html "show_dines",:partial=>'dines/dine_list',:locals=>{:dines=>@dines,:page=>params[:page]}
    end
  end

  def show_task_info
    task = Task.find(params[:user_id])
    @event=task.work_item.event;
    render :partial=>"#{task.link}",:locals=>{:event=>@event}
  end

  def gen_word_by_leader
    @event=Event.find(params[:id])
    @conventioners=@event.conventioners.paginate_all_by_is_leader true,:page => params[:page], :per_page => 5
    response.headers["Content-Type"] = "application/msword"
    render :partial=>'conventioners',:locals=>{:conventioners=>@conventioners,:category=>"leader"}
  end
end
